Source for file Thumbnail.php

Documentation is available at Thumbnail.php

  1. <?php /*
  2.  * FCKeditor - The text editor for internet
  3.  * Copyright (C) 2003-2005 Frederico Caldeira Knabben
  4.  * 
  5.  * Licensed under the terms of the GNU Lesser General Public License:
  6.  *         http://www.opensource.org/licenses/lgpl-license.php
  7.  * 
  8.  * For further information visit:
  9.  *         http://www.fckeditor.net/
  10.  * 
  11.  * File Name: Thumbnail.php
  12.  *     Implements the Thumbnail command, to return
  13.  *     a thumbnail to the browser for the sent file,
  14.  *     if the file is an image an attempt is made to
  15.  *     generate a thumbnail, otherwise an appropriate
  16.  *     icon is returned.
  17.  *     Output is image data
  18.  * 
  19.  * File Authors:
  20.  *         Grant French (grant@mcpuk.net)
  21.  */
  22. include "helpers/iconlookup.php";
  23.  
  24. class Thumbnail {
  25.     var $fckphp_config;
  26.     var $type;
  27.     var $cwd;
  28.     var $actual_cwd;
  29.     var $filename;
  30.     
  31.     function Thumbnail($fckphp_config,$type,$cwd{
  32.         $this->fckphp_config=$fckphp_config;
  33.         $this->type=$type;
  34.         $this->raw_cwd=$cwd;
  35.         $this->actual_cwd=str_replace("//","/",($fckphp_config['UserFilesPath']."/$type/".$this->raw_cwd));
  36.         $this->real_cwd=str_replace("//","/",($this->fckphp_config['basedir']."/".$this->actual_cwd));
  37.         $this->filename=str_replace(array("..","/"),"",$_GET['FileName']);
  38.     }
  39.     
  40.     function run({
  41.         //$mimeIcon=getMimeIcon($mime);
  42.         $fullfile=$this->real_cwd.'/'.$this->filename;
  43.         $thumbfile=$this->real_cwd.'/.thumb_'.$this->filename;
  44.         $icon=false;
  45.         
  46.         if (file_exists($thumbfile)) {
  47.             $icon=$thumbfile;
  48.         else {
  49.             $mime=$this->getMIME($fullfile);
  50.             $ext=strtolower($this->getExtension($this->filename));    
  51.             
  52.             if ($this->isImage($mime,$ext))
  53.                 {
  54.                 
  55.                 //Try and find a thumbnail, else try to generate one
  56.                 //    else send generic picture icon.
  57.                 
  58.                 if ($this->isJPEG($mime,$ext)) {
  59.                     $result=$this->resizeFromJPEG($fullfile);
  60.                     
  61.                 elseif ($this->isGIF($mime,$ext)) {
  62.                     $result=$this->resizeFromGIF($fullfile);
  63.                     
  64.                 elseif ($this->isPNG($mime,$ext)) {
  65.                     $result=$this->resizeFromPNG($fullfile);
  66.                 }
  67.                 
  68.                 if ($result!==false{
  69.                     if (function_exists("imagejpeg")) {
  70.                         imagejpeg($result,$thumbfile,70);
  71.                         chmod($thumbfile,0777);
  72.                         $icon=$thumbfile;
  73.                     elseif (function_exists("imagepng")) {
  74.                         imagepng($result,$thumbfile);
  75.                         chmod($thumbfile,0777);
  76.                         $icon=$thumbfile;
  77.                     elseif (function_exists("imagegif")) {
  78.                         imagegif($result,$thumbfile);
  79.                         chmod($thumbfile,0777);
  80.                         $icon=$thumbfile;
  81.                     else {
  82.                         $icon=iconLookup($mime,$ext);
  83.                     }
  84.                     
  85.                 else {
  86.                     $icon=iconLookup($mime,$ext);
  87.                 }
  88.             else {
  89.                 $icon=iconLookup($mime,$ext);
  90.             }
  91.         }
  92.         
  93.         
  94.         $iconMime=$this->image2MIME($icon);
  95.         if ($iconMime==false$iconMime="image/jpeg";
  96.         
  97.         header("Content-type: $iconMime",true);
  98.         readfile($icon);
  99.         
  100.     }
  101.     
  102.     function getMIME($file{
  103.         $mime="text/plain";
  104.         
  105.         //If mime magic is installed
  106.         if (function_exists("mime_content_type")) {
  107.             $mime=mime_content_type($file);
  108.         else {
  109.             $mime=$this->image2MIME($file);
  110.         }
  111.         
  112.         return strtolower($mime);
  113.     }
  114.     
  115.     function image2MIME($file{
  116.         $fh=fopen($file,"r");
  117.         if ($fh{
  118.             $start4=fread($fh,4);
  119.             $start3=substr($start4,0,3);
  120.             
  121.             if ($start4=="\x89PNG"{
  122.                 return "image/png";
  123.             elseif ($start3=="GIF"{
  124.                 return "image/gif";
  125.             elseif ($start3=="\xFF\xD8\xFF"{
  126.                 return "image/jpeg";
  127.             elseif ($start4=="hsi1"{
  128.                 return "image/jpeg";
  129.             else {
  130.                 return false;
  131.             }
  132.             
  133.             unset($start3);unset($start4);
  134.             fclose($fh);
  135.         else {
  136.             return false;
  137.         }
  138.     }
  139.     
  140.     
  141.     function isImage($mime,$ext{
  142.         if (
  143.             ($mime=="image/gif")||
  144.             ($mime=="image/jpeg")||
  145.             ($mime=="image/jpg")||
  146.             ($mime=="image/pjpeg")||
  147.             ($mime=="image/png")||
  148.             ($ext=="jpg")||
  149.             ($ext=="jpeg")||
  150.             ($ext=="png")||
  151.             ($ext=="gif") ) {
  152.         
  153.             return true;
  154.         else {
  155.             return false;
  156.         }
  157.     }
  158.     
  159.     function isJPEG($mime,$ext{
  160.         if (($mime=="image/jpeg")||($mime=="image/jpg")||($mime=="image/pjpeg")||($ext=="jpg")||($ext=="jpeg")) {
  161.             return true
  162.         else {
  163.             return false;
  164.         }
  165.     }
  166.  
  167.     function isGIF($mime,$ext{
  168.         if (($mime=="image/gif")||($ext=="gif")) {
  169.             return true
  170.         else {
  171.             return false;
  172.         }
  173.     }
  174.     
  175.     function isPNG($mime,$ext{
  176.         if (($mime=="image/png")||($ext=="png")) {
  177.             return true
  178.         else {
  179.             return false;
  180.         }
  181.     }    
  182.     
  183.     function getExtension($filename{
  184.         //Get Extension
  185.         $ext=""
  186.         $lastpos=strrpos($this->filename,'.')
  187.         if ($lastpos!==false$ext=substr($this->filename,($lastpos+1));
  188.         return strtolower($ext);
  189.     }
  190.     
  191.     function resizeFromJPEG($file{
  192.         if (function_exists("imagecreatefromjpeg")) {
  193.             $img=@imagecreatefromjpeg($this->real_cwd.'/'.$this->filename);
  194.             return (($img)?$this->resizeImage($img):false);
  195.         else return false}
  196.     }
  197.     
  198.     function resizeFromGIF($file{
  199.         if (function_exists("imagecreatefromgif")) {
  200.             $img=@imagecreatefromgif($this->real_cwd.'/'.$this->filename);
  201.             return (($img)?$this->resizeImage($img):false);
  202.         else return false}
  203.     }
  204.     
  205.     function resizeFromPNG($file{
  206.         if (function_exists("imagecreatefrompng")) {
  207.             $img=@imagecreatefrompng($this->real_cwd.'/'.$this->filename);
  208.             return (($img)?$this->resizeImage($img):false);
  209.         else return false}
  210.     }
  211.     
  212.     function resizeImage($img{
  213.         //Get size for thumbnail
  214.         $width=imagesx($img)$height=imagesy($img);
  215.         if ($width>$height$n_height=$height*(96/$width)$n_width=96else $n_width=$width*(96/$height)$n_height=96}
  216.         
  217.         $x=0;$y=0;
  218.         if ($n_width<96$x=round((96-$n_width)/2);
  219.         if ($n_height<96$y=round((96-$n_height)/2);
  220.         
  221.         $thumb=imagecreatetruecolor(96,96);
  222.         
  223.         #Background colour fix by:
  224.         #Ben Lancaster (benlanc@ster.me.uk)
  225.         $bgcolor imagecolorallocate($thumb,255,255,255);
  226.         imagefill($thumb00$bgcolor);
  227.         
  228.         if (function_exists("imagecopyresampled")) {
  229.             if (!($result=@imagecopyresampled($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height))) {
  230.                 $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
  231.             }    
  232.         else {
  233.             $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
  234.         }
  235.  
  236.         return ($result)?$thumb:false;
  237.     }
  238. }
  239.  
  240. ?>

Documentation generated on Mon, 05 May 2008 16:23:11 +0400 by phpDocumentor 1.4.0